<?php

include_once( rootpath . 'Smarty.class.php');
$main_smarty = new Smarty;

include( rootpath . 'config.php');
include(mnminclude.'html1.php');
include(mnminclude.'link.php');
include(mnminclude.'tags.php');
include(mnminclude.'search.php');
include(mnminclude.'smartyvariables.php');


$active_newsletter = $main_smarty->get_config_vars('PLIGG_Visual_Mail_ActiveNewsletter');

$page_text = $main_smarty->get_config_vars('PLIGG_Visual_Mail_Text');
if( $page_text == 'false' )
{
    $sql = "SELECT * from ".table_links." where link_title_url='$active_newsletter' and link_status='page'";
    $page = (array)$db->get_row($sql);
    $page_text = $page['link_content'];
}

if( !$page_text )
    exit();

$sql = "SELECT * FROM " . table_users . " WHERE user_login IN('bobyk', 'MassimoUA') AND sent_newsletter IS NULL ORDER BY user_id ASC LIMIT " . $main_smarty->get_config_vars('PLIGG_Visual_Mail_SendLimit');;
$users = $db->get_results($sql);

foreach( $users as $user )
{
    $user = (array)$user;
    
    sendMail( 
	$user['user_email'], 
	$main_smarty->get_config_vars('PLIGG_Visual_Mail_Subject'), 
	$page_text
    );
    
    $user_id = $user['user_id'];
    $sql = "UPDATE " . table_users . " SET sent_newsletter=1 WHERE user_id='$user_id';";
    $db->get_results($sql);
}

function sendMail( $to, $subject, $html )
{
    $_headers  = 'MIME-Version: 1.0' . "\r\n";
    $_headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

    $_html = '<html><header></header><body style="font-size: 15px; font-family: arial,sans-serif;">'. $html .'<div style="margin-top: 20px;"><a href="http://tipsrate.com/">TipsRate.com</a></div></body></html>';
    
    return mb_send_mail( $to, $subject, $_html, $_headers );
}